/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.spring.form.component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.skatiger.jrove.ViewContext;
import org.springframework.util.Assert;

/**
 * @author skatiger
 *
 */
public class FormErrors extends HtmlFormComponent {
    public static final String MESSAGES_ATTRIBUTE = "messages";
    /**
    * The HTML '<code>span</code>' tag.
    */
    public static final String SPAN_TAG = "span";
    public static final String BR_TAG = "<br />";
    private String element;
    private String delimiter;
    private boolean errorMessagesWereExposed;

    private transient Object previousMessages;

    /**
     * Set the HTML element must be used to render the error messages.
     * <p>Defaults to an HTML '<code>&lt;span/&gt;</code>' tag.
     */
    public void setElement(String element) {
        Assert.hasText(element, "'element' cannot be null or blank");
        this.element = element;
    }

    public String getElement() {
        return getPropertyValue("element", element, SPAN_TAG);
    }

    /**
     * Set the delimiter to be used between error messages.
     * <p>Defaults to an HTML '<code>&lt;br/&gt;</code>' tag.
     */
    public void setDelimiter(String delimiter) {
        this.delimiter = delimiter;
    }

    public String getDelimiter() {
        return getPropertyValue("delimiter", delimiter, ",");
    }

    /**
    * Get the value for the HTML '<code>name</code>' attribute.
    * <p>Simply returns <code>null</code> because the '<code>name</code>' attribute
    * is not a validate attribute for the '<code>span</code>' element.
    */
    @Override
    public String getName() {
        return null;
    }

    //    @Override
    //    public boolean isRendered() {
    //        try {
    //            return getBindStatus().isError();
    //        } catch (IllegalStateException ex) {
    //            // Neither BindingResult nor target object available.
    //            return super.isRendered();
    //        }
    //    }

    public void exposeAttributes(ViewContext context) {
        List<String> errorMessages = new ArrayList<String>();
        errorMessages.addAll(Arrays.asList(getBindStatus().getErrorMessages()));
        this.previousMessages = context.getAttribute(MESSAGES_ATTRIBUTE);
        context.setAttribute(MESSAGES_ATTRIBUTE, errorMessages);
        this.errorMessagesWereExposed = true;
    }

    public void removeAttributes(ViewContext context) {
        if (this.errorMessagesWereExposed) {
            if (this.previousMessages != null) {
                context.setAttribute(MESSAGES_ATTRIBUTE, this.previousMessages);
                this.previousMessages = null;
            } else {
                context.removeAttribute(MESSAGES_ATTRIBUTE);
            }
        }
    }

    /* (non-Javadoc)
     * @see org.skatiger.jrove.component.UIComponentBase#encodeBegin(org.skatiger.jrove.ViewContext)
     */
    @Override
    public void encodeBegin(ViewContext context) throws IOException {
        exposeAttributes(context);
        super.encodeBegin(context);
    }

    /* (non-Javadoc)
     * @see org.skatiger.jrove.component.UIComponentBase#encodeEnd(org.skatiger.jrove.ViewContext)
     */
    @Override
    public void encodeEnd(ViewContext context) throws IOException {
        super.encodeEnd(context);
        removeAttributes(context);
    }
}
